﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.Threading;
using System.Security.Cryptography;
using System.Windows.Threading;
using System.Windows;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using System.Collections.ObjectModel;
using System.Windows.Media;

namespace WpfImageDuplicates
{
    [Flags]
    public enum FilterStatus
    {
        None = 0,
        Disabled = 1,
    }

    [Serializable]
    public class SingleFilter : INotifyPropertyChanged
    {
        protected object _lockObj = new object();

        public SingleFilter() { }
        public SingleFilter(IFilter filter)
        {
            Filter = filter;
        }

        [NonSerialized]
        protected IFilter _filter;

        [NonSerialized]
        protected Type _filterType;

        protected FilterStatus _status;

        protected string _filterTypeName;

        public string FilterTypeName
        {
            get { return _filterTypeName; }
            set
            {
                if (_filterTypeName != value)
                {
                    _filterTypeName = value;
                    OnPropertyChanged("FilterTypeName");
                }
            }
        }

        public FilterStatus Status
        {
            get { return _status; }
            set
            {
                if (_status != value)
                {
                    _status = value;
                    OnPropertyChanged("Status");
                }
            }
        }

        public Type FilterType
        {
            get
            {
                if (_filterType == null)
                {
                    lock (_lockObj)
                    {
                        if (_filterType == null)
                        {
                            _filterType = AllFilters.FindFilter(_filterTypeName);
                        }
                    }
                }
                return _filterType;
            }
            set
            {
                if (_filterType != value)
                {
                    _filterType = value;
                    OnPropertyChanged("FilterType");
                }
            }
        }

        public IFilter Filter
        {
            get
            {
                if (_filter == null)
                {
                    lock (_lockObj)
                    {
                        if (_filter == null)
                        {
                            Type t = FilterType;

                            if (t != null)
                            {
                                _filter = t.CreateFilter();
                            }
                        }
                    }
                }
                return _filter;
            }
            set
            {
                if (_filter != value)
                {
                    _filter = value;
                    FilterType = value.GetType();
                    FilterTypeName = _filterType.Name;
                    OnPropertyChanged("Filter");
                }
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region INotifyPropertyChanged Members

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}

